package me.snowiest.snowdrift.controller;

import me.snowiest.snowdrift.entity.Tag;
import me.snowiest.snowdrift.service.TagService;
import me.snowiest.snowdrift.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 新增tag
     */
    @PostMapping
    public CommonResult a(@RequestBody Map<String, Object> parameterMap) {
        try {
            Tag tag = new Tag(parameterMap);
            tagService.addTag(tag);
            return new CommonResult(1, "success", "");
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(-1, "fail", null);
        }
    }

    @PostMapping("/{id}")
    public CommonResult a(@PathVariable("id") Integer id, @RequestBody Tag tag) {
        return new CommonResult(-1, "fail", "unsupported method");
    }

    /**
     * 删除tag
     */
    @DeleteMapping
    public CommonResult b(@RequestParam("id") Integer id) {
        try {
            tagService.removeTagById(id);
            return new CommonResult(1, "success", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(-1, "fail", null);
        }
    }

    @DeleteMapping("/{id}")
    public CommonResult f(@PathVariable("id") Integer id) {
        try {
            tagService.removeTagById(id);
            return new CommonResult(1, "success", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(-1, "fail", null);
        }
    }

    /**
     * 修改tag
     */
    @PutMapping
    public CommonResult c(@RequestBody Tag tag) {
        try {
            tagService.saveTag(tag);
            return new CommonResult(1, "success", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(-1, "fail", null);
        }
    }

    @PutMapping("/{id}")
    public CommonResult g(@RequestBody Tag tag) {
        try {
            tagService.saveTag(tag);
            return new CommonResult(1, "success", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(-1, "fail", null);
        }
    }

    /**
     * 获取tag
     */
    @GetMapping
    public CommonResult d(@RequestParam(value = "id", required = false) Integer id) {
        if (Objects.nonNull(id)) {
            try {
                Tag tag = tagService.getTagById(id);
                return new CommonResult(1, "success", tag);
            } catch (Exception e) {
                e.printStackTrace();
                return new CommonResult(-1, "fail", null);
            }
        }

        try {
            List<Tag> tagList = tagService.getAllTag();
            return new CommonResult(1, "success", tagList);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(-1, "fail", null);
        }
    }

    @GetMapping("/{id}")
    public CommonResult e(@PathVariable("id") Integer id) {
        try {
            Tag tag = tagService.getTagById(id);
            return new CommonResult(1, "success", tag);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(-1, "fail", null);
        }
    }
}
